package cn.stronglink.asset.module.datacount.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.data.category.DefaultCategoryDataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.module.datacount.DataCountService;
import cn.stronglink.asset.module.datacount.vo.SearchParam;
import cn.stronglink.asset.module.report.vo.ExlChartVo;
import cn.stronglink.suite.core.util.DownloadExcelUtil;
import jxl.format.BorderLineStyle;

@Controller
@RequestMapping(value = "dataExcel")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DataCountExcelController {

	private static final Logger logger = LoggerFactory.getLogger(DataCountExcelController.class);
	
	@Autowired
	private DataCountService dataCountService;
	
	@RequestMapping(value = "exportAssetType")
	public String exportAssetType(HttpServletRequest request,HttpServletResponse response,Model model){

		try{
			String p = request.getParameter("param").toString();
			SearchParam param=new SearchParam();
			if(p!=null&&!"".equals(p)){
				String paramString = new String(p.getBytes("ISO8859-1"), "UTF-8");
				param=JSON.parseObject(paramString,SearchParam.class);
			}
			logger.info(JSON.toJSONString(param)+"参数1");
			HashMap mapData=dataCountService.getAssetTypeCount(param);
			response.setContentType("application/msexcel;charset=UTF-8");
			// 第一步，创建一个webbook，对应一个Excel文件
			List<HashMap> dataList=(List<HashMap>) mapData.get("dataList");
			List<String> valueIds=(List<String>) mapData.get("valueIds");
			List<String> labels=(List<String>) mapData.get("labels");
			String imgPath =null;
			DownloadExcelUtil dExlUtile =new DownloadExcelUtil(response,"资产类型","资产类型台账");
			DefaultCategoryDataset mDataset = null;
			if(labels!=null&&labels.size()>0) {
				String[] titleArray = new String[labels.size()];
				for (int i = 0; i < labels.size(); i++) {
					titleArray[i] = labels.get(i);
				}
				dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			if (dataList!=null && dataList.size()>0) {
				for (HashMap map : dataList) {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j<valueIds.size();j++){
						if(j==0){
							sb.append(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():null);
							sb.append("$");
						}else{
							sb.append(Integer.parseInt(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():"0"));
							sb.append("$");
						}
					}
					String tvsb = sb.toString();
					String[] tvsbArray = tvsb.split("\\$");
					dExlUtile.addRow(tvsbArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
				
				List<ExlChartVo> exlChartList = (List<ExlChartVo>) mapData.get("exlChartList");
				if (dataList != null && dataList.size()>0) {
					mDataset = new DefaultCategoryDataset();
					for (ExlChartVo exlChartVo : exlChartList) {
						if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
							exlChartVo.setColumnKey("无资产类型");
						}
						mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
					}
				}
				
				if (mDataset!=null) {
					dExlUtile.addSheet("资产类型图");
					imgPath = dExlUtile.drawBarOnSheet("资产类型图","资产类型", "数量", mDataset);
				}
				dExlUtile.reportExcel();
				File imgFile = new File(imgPath);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}else {
				String[] errArray = new String[1];
				errArray[0] = "查询结果为空";
				dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	
	@RequestMapping(value = "exportAssetStatus")
	public String exportAssetStatus(HttpServletRequest request,HttpServletResponse response,Model model){
		try{
			String p = request.getParameter("param").toString();
			SearchParam param=new SearchParam();
			if(p!=null&&!"".equals(p)){
				String paramString = new String(p.getBytes("ISO8859-1"), "UTF-8");
				param=JSON.parseObject(paramString,SearchParam.class);
			}
			logger.info(JSON.toJSONString(param)+"参数2");
			HashMap mapData=dataCountService.getAssetStatusCount(param);
			response.setContentType("application/msexcel;charset=UTF-8");
			List<HashMap> dataList=(List<HashMap>) mapData.get("dataList");
			List<String> valueIds=(List<String>) mapData.get("valueIds");
			List<String> labels=(List<String>) mapData.get("labels");
			String imgPath =null;
			DownloadExcelUtil dExlUtile =new DownloadExcelUtil(response,"资产状态","资产状态台账");
			DefaultCategoryDataset mDataset = null;
			if(labels!=null&&labels.size()>0) {
				String[] titleArray = new String[labels.size()];
				for (int i = 0; i < labels.size(); i++) {
					titleArray[i] = labels.get(i);
				}
				dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			if (dataList!=null && dataList.size()>0) {
				for (HashMap map : dataList) {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j<valueIds.size();j++){
						if(j==0){
							sb.append(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():null);
							sb.append("$");
						}else{
							sb.append(Integer.parseInt(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():"0"));
							sb.append("$");
						}
					}
					String tvsb = sb.toString();
					String[] tvsbArray = tvsb.split("\\$");
					dExlUtile.addRow(tvsbArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
				
				List<ExlChartVo> exlChartList = (List<ExlChartVo>) mapData.get("exlChartList");
				if (dataList != null && dataList.size()>0) {
					mDataset = new DefaultCategoryDataset();
					for (ExlChartVo exlChartVo : exlChartList) {
						if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
							exlChartVo.setColumnKey("无资产状态");
						}
						mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
					}
				}
				
				if (mDataset!=null) {
					dExlUtile.addSheet("资产状态图");
					imgPath = dExlUtile.drawBarOnSheet("资产状态图","资产状态", "数量", mDataset);
				}
				dExlUtile.reportExcel();
				File imgFile = new File(imgPath);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}else {
				String[] errArray = new String[1];
				errArray[0] = "查询结果为空";
				dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	
	@RequestMapping(value = "exportAssetBrand")
	public String exportAssetBrand(HttpServletRequest request,HttpServletResponse response,Model model){

		try{
			String p = request.getParameter("param").toString();
			SearchParam param=new SearchParam();
			if(p!=null&&!"".equals(p)){
				String paramString = new String(p.getBytes("ISO8859-1"), "UTF-8");
				param=JSON.parseObject(paramString,SearchParam.class);
			}
			logger.info(JSON.toJSONString(param)+"参数2");
			HashMap mapData=dataCountService.getAssetBrandCount(param);
			response.setContentType("application/msexcel;charset=UTF-8");
			List<HashMap> dataList=(List<HashMap>) mapData.get("dataList");
			List<String> valueIds=(List<String>) mapData.get("valueIds");
			List<String> labels=(List<String>) mapData.get("labels");
			String imgPath =null;
			DownloadExcelUtil dExlUtile =new DownloadExcelUtil(response,"资产品牌","资产品牌台账");
			DefaultCategoryDataset mDataset = null;
			if(labels!=null&&labels.size()>0) {
				String[] titleArray = new String[labels.size()];
				for (int i = 0; i < labels.size(); i++) {
					titleArray[i] = labels.get(i);
				}
				dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			if (dataList!=null && dataList.size()>0) {
				for (HashMap map : dataList) {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j<valueIds.size();j++){
						if(j==0){
							sb.append(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():null);
							sb.append("$");
						}else{
							sb.append(Integer.parseInt(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():"0"));
							sb.append("$");
						}
					}
					String tvsb = sb.toString();
					String[] tvsbArray = tvsb.split("\\$");
					dExlUtile.addRow(tvsbArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
				
				List<ExlChartVo> exlChartList = (List<ExlChartVo>) mapData.get("exlChartList");
				if (dataList != null && dataList.size()>0) {
					mDataset = new DefaultCategoryDataset();
					for (ExlChartVo exlChartVo : exlChartList) {
						if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
							exlChartVo.setColumnKey("无资产品牌");
						}
						mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
					}
				}
				
				if (mDataset!=null) {
					dExlUtile.addSheet("资产品牌图");
					imgPath = dExlUtile.drawBarOnSheet("资产品牌图","资产品牌", "数量", mDataset);
				}
				dExlUtile.reportExcel();
				File imgFile = new File(imgPath);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}else {
				String[] errArray = new String[1];
				errArray[0] = "查询结果为空";
				dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	
	@RequestMapping(value = "exportAssetDepart")
	public String exportAssetDepart(HttpServletRequest request,HttpServletResponse response,Model model){

		try{
			String p = request.getParameter("param").toString();
			SearchParam param=new SearchParam();
			if(p!=null&&!"".equals(p)){
				String paramString = new String(p.getBytes("ISO8859-1"), "UTF-8");
				param=JSON.parseObject(paramString,SearchParam.class);
			}
			logger.info(JSON.toJSONString(param)+"参数2");
			HashMap mapData=dataCountService.getAssetDepartCount(param);
			response.setContentType("application/msexcel;charset=UTF-8");
			List<HashMap> dataList=(List<HashMap>) mapData.get("dataList");
			List<String> valueIds=(List<String>) mapData.get("valueIds");
			List<String> labels=(List<String>) mapData.get("labels");
			String imgPath =null;
			DownloadExcelUtil dExlUtile =new DownloadExcelUtil(response,"资产领用部门","资产领用部门台账");
			DefaultCategoryDataset mDataset = null;
			if(labels!=null&&labels.size()>0) {
				String[] titleArray = new String[labels.size()];
				for (int i = 0; i < labels.size(); i++) {
					titleArray[i] = labels.get(i);
				}
				dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			if (dataList!=null && dataList.size()>0) {
				for (HashMap map : dataList) {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j<valueIds.size();j++){
						if(j==0){
							sb.append(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():null);
							sb.append("$");
						}else{
							sb.append(Integer.parseInt(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():"0"));
							sb.append("$");
						}
					}
					String tvsb = sb.toString();
					String[] tvsbArray = tvsb.split("\\$");
					dExlUtile.addRow(tvsbArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
				
				List<ExlChartVo> exlChartList = (List<ExlChartVo>) mapData.get("exlChartList");
				if (dataList != null && dataList.size()>0) {
					mDataset = new DefaultCategoryDataset();
					for (ExlChartVo exlChartVo : exlChartList) {
						if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
							exlChartVo.setColumnKey("无资产领用部门");
						}
						mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
					}
				}
				
				if (mDataset!=null) {
					dExlUtile.addSheet("资产领用部门图");
					imgPath = dExlUtile.drawBarOnSheet("资产领用部门图","资产领用部门", "数量", mDataset);
				}
				dExlUtile.reportExcel();
				File imgFile = new File(imgPath);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}else {
				String[] errArray = new String[1];
				errArray[0] = "查询结果为空";
				dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	
	@RequestMapping(value = "exportAssetServiceLife")
	public String exportAssetServiceLife(HttpServletRequest request,HttpServletResponse response,Model model){

		try{
			String p = request.getParameter("param").toString();
			SearchParam param=new SearchParam();
			if(p!=null&&!"".equals(p)){
				String paramString = new String(p.getBytes("ISO8859-1"), "UTF-8");
				param=JSON.parseObject(paramString,SearchParam.class);
			}
			logger.info(JSON.toJSONString(param)+"参数2");
			HashMap mapData=dataCountService.serviceLifeCount(param);
			response.setContentType("application/msexcel;charset=UTF-8");
			List<HashMap> dataList=(List<HashMap>) mapData.get("dataList");
			List<String> valueIds=(List<String>) mapData.get("valueIds");
			List<String> labels=(List<String>) mapData.get("labels");
			String imgPath =null;
			DownloadExcelUtil dExlUtile =new DownloadExcelUtil(response,"使用期限","使用期限台账");
			DefaultCategoryDataset mDataset = null;
			if(labels!=null&&labels.size()>0) {
				String[] titleArray = new String[labels.size()];
				for (int i = 0; i < labels.size(); i++) {
					titleArray[i] = labels.get(i);
				}
				dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			if (dataList!=null && dataList.size()>0) {
				for (HashMap map : dataList) {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j<valueIds.size();j++){
						if(j==0){
							sb.append(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():null);
							sb.append("$");
						}else{
							sb.append(Integer.parseInt(map.get(valueIds.get(j))!=null?map.get(valueIds.get(j)).toString():"0"));
							sb.append("$");
						}
					}
					String tvsb = sb.toString();
					String[] tvsbArray = tvsb.split("\\$");
					dExlUtile.addRow(tvsbArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
				
				List<ExlChartVo> exlChartList = (List<ExlChartVo>) mapData.get("exlChartList");
				if (dataList != null && dataList.size()>0) {
					mDataset = new DefaultCategoryDataset();
					for (ExlChartVo exlChartVo : exlChartList) {
						if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
							exlChartVo.setColumnKey("无使用期限统计");
						}
						mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
					}
				}
				
				if (mDataset!=null) {
					dExlUtile.addSheet("使用期限图");
					imgPath = dExlUtile.drawBarOnSheet("使用期限图","使用期限", "数量", mDataset);
				}
				dExlUtile.reportExcel();
				File imgFile = new File(imgPath);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}else {
				String[] errArray = new String[1];
				errArray[0] = "查询结果为空";
				dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	
	
	@RequestMapping(value = "exportAssetCabinetCount")
	public String exportAssetCabinetCount(HttpServletRequest request,HttpServletResponse response,Model model){

		try{
			String p = request.getParameter("param").toString();
			SearchParam param=new SearchParam();
			if(p!=null&&!"".equals(p)){
				String paramString = new String(p.getBytes("ISO8859-1"), "UTF-8");
				param=JSON.parseObject(paramString,SearchParam.class);
			}
			logger.info(JSON.toJSONString(param)+"参数2");
			HashMap mapData=dataCountService.CabinetCount(param);
			response.setContentType("application/msexcel;charset=UTF-8");
			List<HashMap> dataList=(List<HashMap>) mapData.get("dataList");
			String imgPath =null;
			DownloadExcelUtil dExlUtile =new DownloadExcelUtil(response,"机柜使用情况","机柜使用情况账");
			DefaultCategoryDataset mDataset = null;
			String[] titleArray = new String[4];
			titleArray[0] = "机房名称";
			titleArray[1] = "0%-40%";
			titleArray[2] = "40%-60%";
			titleArray[3] = "60%-100%";
			dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			if (dataList!=null && dataList.size()>0) {
				for (HashMap map : dataList) {
					StringBuffer sb = new StringBuffer();
					sb.append(map.get("roomName").toString()!=null?map.get("roomName").toString():"");
					sb.append("$");
					sb.append(map.get("oneStage").toString());
					sb.append("$");
					sb.append(map.get("twoStage").toString());
					sb.append("$");
					sb.append(map.get("threeStage").toString());
					String tvsb = sb.toString();
					String[] tvsbArray = tvsb.split("\\$");
					dExlUtile.addRow(tvsbArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
				
				List<ExlChartVo> exlChartList = (List<ExlChartVo>) mapData.get("exlChartList");
				if (dataList != null && dataList.size()>0) {
					mDataset = new DefaultCategoryDataset();
					for (ExlChartVo exlChartVo : exlChartList) {
						if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
							exlChartVo.setColumnKey("无机房使用情况统计");
						}
						mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
					}
				}
				
				if (mDataset!=null) {
					dExlUtile.addSheet("机房使用情况图");
					imgPath = dExlUtile.drawBarOnSheet("机房使用情况图","使用情况", "数量", mDataset);
				}
				dExlUtile.reportExcel();
				File imgFile = new File(imgPath);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}else {
				String[] errArray = new String[1];
				errArray[0] = "查询结果为空";
				dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	
	
}